﻿using System;
using System.Configuration;

namespace Gem.Configuration
{
    public class SccEnvironmentsConfigCollection : ConfigurationElementCollection
    {
        public SccEnvironmentsConfigCollection()
        {
            AddElementName = "SccEnvironment";
        }

        #region ConfigurationElementCollection overrides

        protected override ConfigurationElement CreateNewElement()
        {
            return new SccEnvironmentConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            var sccEnvironmentElement = element as SccEnvironmentConfigElement;

            if (sccEnvironmentElement == null)
            {
                throw new ArgumentException("element");
            }

            return sccEnvironmentElement.Folder;
        }

        #endregion

        #region Collection operations

        public SccEnvironmentConfigElement this[int index]
        {
            get
            {
                return (SccEnvironmentConfigElement)BaseGet(index);
            }

            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }

                BaseAdd(index, value);
            }
        }

        public void Add(SccEnvironmentConfigElement environmentConfig)
        {
            BaseAdd(environmentConfig);
        }

        public void Clear()
        {
            BaseClear();
        }

        public void Remove(SccEnvironmentConfigElement environmentConfig)
        {
            BaseRemove(GetElementKey(environmentConfig));
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        #endregion
    }
}
